﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Net;

namespace Pingy
{
    /// <summary>
    /// Interaction logic for WhoIs.xaml
    /// </summary>
    public partial class WhoIs : Window, INotifyPropertyChanged
    {
        string address;
        public string Address
        {
            get { return address; }
            set { address = value; SendPropertyChanged("Address"); }
        }

        private string[] servers = new string[] { WHOISServers.Ripe, WHOISServers.ARIN, WHOISServers.Internic };
        public string[] Servers
        {
            get { return servers; }
            set { servers = value; }
        }

        public WhoIs()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        #region INotifyPropertyChanged Members

        public void SendPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void lookup_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(serverBox.Text))
                {
                    MessageBox.Show(this, "You need to select a server to query.", "Missing server", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }
                if (string.IsNullOrEmpty(Address))
                {
                    MessageBox.Show(this, "You need to enter an IP or a host name.", "Missing address", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }

                string reply = Network.WHOIS(Address, serverBox.Text);
                whoisResult.Text = reply;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.ToString(), "Query failed", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
